package com.fowo.api.sms.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fowo.api.sms.config.SmsServiceProperties;
import com.fowo.api.sms.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Dictionary;
import java.util.Enumeration;

@Slf4j
@EnableConfigurationProperties(SmsServiceProperties.class)
@Service
public class AliyunSmsServiceImpl implements SmsService {
    @Resource
    private SmsServiceProperties serviceProperties;
    @Resource
    private ObjectMapper objectMapper;

    private Client createClient() throws Exception {
        Config config = new Config()
                .setAccessKeyId(serviceProperties.getAccessKeyId())
                .setAccessKeySecret(serviceProperties.getAccessKeySecret());
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

    @Override
    public void sendSms(String scene, String phone, Dictionary<String, Object> params) throws Exception {
        SmsServiceProperties.SmsSceneTemplate smsSceneTemplate = serviceProperties.getScenes().get(scene);
        if (smsSceneTemplate == null) {
            throw new Exception(String.format("未能找到场景值“%s”对应的短信模版配置！", scene));
        }

        if (smsSceneTemplate.getParamNameMaps() != null) {
            // 参数名替换处理
            final Enumeration<String> keys = params.keys();
            while(keys.hasMoreElements()) {
                String nextKey = keys.nextElement();
                if (smsSceneTemplate.getParamNameMaps().containsKey(nextKey)) {
                    params.put(smsSceneTemplate.getParamNameMaps().get(nextKey), params.get(nextKey));
                    params.remove(nextKey);
                }
            }
        }

        String templateParam = objectMapper.writeValueAsString(params);

        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(serviceProperties.getSign())
                .setTemplateCode(smsSceneTemplate.getTemplateCode())
                .setPhoneNumbers(phone)
                .setTemplateParam(templateParam);
        SendSmsResponse sendSmsResponse = createClient().sendSms(sendSmsRequest);
        log.debug("sendSmsResponse: {}", sendSmsResponse);
    }

    @Override
    public void sendSmsByTemplate(String templateCode, String phone, Dictionary<String, Object> params) throws Exception {
        String templateParam = objectMapper.writeValueAsString(params);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(serviceProperties.getSign())
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam(templateParam);
        SendSmsResponse sendSmsResponse = createClient().sendSms(sendSmsRequest);
        log.debug("sendSmsResponse: {}", sendSmsResponse);
    }
}
